package com.dq.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.dq.commonutils.pojo.DqException;
import com.dq.vod.properties.AliyunVodProperties;
import com.dq.vod.service.VodService;
import com.dq.vod.util.InitAliyunClient;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @project: guli_parent
 * @ClassName: VodServiceImpl
 * @author: dq
 * @creat: 2022/7/29 19:32
 */
@Service
public class VodServiceImpl implements VodService {
    @Autowired
    private AliyunVodProperties vodProperties;

    @Override
    public String uploadAliyunVideo(MultipartFile file) {
        /**
         * 1. 获取文件流
         * 2. 拼接视频名称  2022-7-29::asdasdaff.mp4
         * 3. uploader上传请求（request）
         * 4. 获取response中的视频id
         * 5. 返回id
         */
        try {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String nowDate = new DateTime().toString("yyyy-MM-dd");
            //2022-7-29::asdasdaff.mp4
            String finName = nowDate + "::" + uuid + fileType;

            UploadStreamRequest request = new UploadStreamRequest(vodProperties.getKeyId(), vodProperties.getKeySecret(), finName, file.getOriginalFilename(), file.getInputStream());

            /* 是否使用默认水印(可选)，指定模板组ID时，根据模板组配置确定是否使用默认水印*/
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            String viedoId = null;
            if (response.isSuccess()) {
                viedoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                viedoId = response.getVideoId();
            }
            return viedoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void deleteAliyunViedoByVedioId(String videoSourceId) {
        /**
         * 1. 获取视频id
         * 2. 注册vodClient
         * 3. 创建一个request对象 设置属性
         * 4. client发送请求
         */
        try {
            //设置request请求
            DefaultAcsClient client = InitAliyunClient.initVodClient(vodProperties.getKeyId(), vodProperties.getKeySecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoSourceId);
            DeleteVideoResponse response = client.getAcsResponse(request);

        } catch (ClientException e) {
            e.printStackTrace();
            throw new DqException(20001, "删除视频失败");
        }
    }

    @Override
    public void deleteAliyunVideoBatch(List videoIdList) {
        /**
         * 1. 把videoList 从[11, 11, 11, 11]变为 11,11,11,11这种格式
         * 2. 使用request.setvedioId()
         * 3. 删除
         */
        try {
            //设置request请求
            DefaultAcsClient client = InitAliyunClient.initVodClient(vodProperties.getKeyId(), vodProperties.getKeySecret());
            DeleteVideoRequest request = new DeleteVideoRequest();
            String videoIdBatch = StringUtils.join(videoIdList.toArray(), ",");
            request.setVideoIds(videoIdBatch);
            DeleteVideoResponse response = client.getAcsResponse(request);

        } catch (ClientException e) {
            e.printStackTrace();
            throw new DqException(20001, "删除视频失败");
        }
    }

    //获取视频凭证
    @Override
    public String getPlayAuth(String id) {
        try {
            DefaultAcsClient client = InitAliyunClient.initVodClient(vodProperties.getKeyId(), vodProperties.getKeySecret());
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return playAuth;
        } catch (ClientException e) {
            e.printStackTrace();
            throw new DqException(20001,"获取凭证异常");
        }
    }

    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();
        arrayList.add("11");
        arrayList.add("11");
        arrayList.add("11");
        arrayList.add("11");
        System.out.println(arrayList);
        String join = StringUtils.join(arrayList.toArray(), ",");
        System.out.println(join);
    }
}
